<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>客户管理</el-breadcrumb-item>
      <el-breadcrumb-item v-for="item in history" :key="item.id"><span v-text="item.name"></span>
      </el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <div style="margin-bottom: 50px;">
      <el-button type="primary" size="medium"
                 @click="$router.push('/sys-admin/client/add-new')">添加客户
      </el-button>
      <div style="width: 250px;float: right">
        <el-input v-model="wd" placeholder="请输入内容" clearable></el-input>
        <el-button style="float: right;margin-top: 10px" size="mini" @click="query()" slot="append">查询</el-button>
      </div>
    </div>

    <el-table ref="tableData" :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="60" align="center"></el-table-column>
      <el-table-column prop="name" label="名称" width="100" header-align="center"
                       show-overflow-tooltip></el-table-column>
      <el-table-column prop="petName" label="宠物名称" width="100" align="center" header-align="center"
                       show-overflow-tooltip>
        <template :data="pet" slot-scope="scope">
          <el-button type="text" @click="standardByName(scope.row)">{{ scope.row.petName }}</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="phone" label="手机号" width="180" header-align="center"
                       show-overflow-tooltip></el-table-column>
      <el-table-column prop="email" label="电子邮箱" width="180" header-align="center"
                       show-overflow-tooltip></el-table-column>
      <el-table-column prop="address" label="地址" header-align="center"
                       show-overflow-tooltip></el-table-column>
      <el-table-column prop="description" label="备注" header-align="center"
                       show-overflow-tooltip></el-table-column>
      <el-table-column label="VIP" prop="vip" width="100" align="center">
        <template slot-scope="scope">
          <el-switch
              @change="toggleVip(scope.row)"
              v-model="scope.row.vip"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#cccccc">
          </el-switch>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit" circle
                     @click="openEditDialog(scope.row)"></el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" circle
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 返回上层 -->
    <div v-if="willShow" style="margin-top: 10px; text-align: right;">
      <el-button size="small" @click="goBack()">返回</el-button>
    </div>

    <div v-if="paginationShow" style="text-align: right; margin: 10px auto;">
      <el-pagination
          @current-change="changePage"
          layout="total, prev, pager, next"
          :total="total"
          :current-page="currentPage"
          :page-size="pageSize">
      </el-pagination>
    </div>
    <!--修改弹窗-->
    <el-dialog title="修改客户" :visible.sync="dialogFormVisible">
      <el-form :model="editForm" :rules="rules" ref="ruleForm" label-width="100px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="宠物名称" prop="petName">
          <el-input v-model="editForm.petName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="editForm.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电子邮箱" prop="email">
          <el-input v-model="editForm.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="description">
          <el-input v-model="editForm.description" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm">确 定</el-button>
      </div>
    </el-dialog>
    <!--宠物信息弹窗-->
    <el-dialog
        title="宠物信息"
        :visible.sync="dialogVisible"
        width="30%"
    >
      <el-form :model="pet" :rules="rules" ref="ruleForm" label-width="100px">
        <el-form-item label="名称" prop="name">
          <el-input :disabled="true" v-model="pet.name"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="xingbie">
          <el-input :disabled="true" v-model="pet.xingbie"></el-input>
        </el-form-item>
        <el-form-item label="出生日" prop="birth">
          <el-input :disabled="true" v-model="pet.birth"></el-input>
        </el-form-item>
        <el-form-item label="发色" prop="colour">
          <el-input :disabled="true" v-model="pet.colour"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="xingbie">
          <el-input :disabled="true" v-model="pet.xingbie"></el-input>
        </el-form-item>
        <el-form-item label="出生地" prop="chushengdi">
          <el-input :disabled="true" v-model="pet.chushengdi"></el-input>
        </el-form-item>
        <el-form-item label="疫苗" prop="yimiao">
          <el-input :disabled="true" v-model="pet.yimiao"></el-input>
        </el-form-item>
        <el-form-item label="血统书" prop="xuetong">
          <el-input :disabled="true" v-model="pet.xuetong"></el-input>
        </el-form-item>
        <el-form-item label="价格" prop="jiage">
          <el-input :disabled="true" v-model="pet.jiage"></el-input>
        </el-form-item>
        <el-form-item label="体重" prop="tizhong">
          <el-input :disabled="true" v-model="pet.tizhong"></el-input>
        </el-form-item>
        <el-form-item label="体重测试日" prop="tzceshiri">
          <el-input :disabled="true" v-model="pet.tzceshiri"></el-input>
        </el-form-item>
        <el-form-item label="性格" prop="xingge">
          <el-input :disabled="true" v-model="pet.xingge"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer" :data="pet">
         <el-button @click="gotoSkuManagement(pet.name)">宠物管理</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </div>

    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pet: [],
      dialogVisible: false,
      willShow: false,
      paginationShow: true,
      tableData: [],
      total: 0,
      pageSize: 0,
      currentPage: 1,
      dialogFormVisible: false,
      // 浏览历史，此数据主要用于返回功能
      history: [],
      wd: "",
      editForm: {
        id: '',
        name: '',
        phone: '',
        email: '',
        vip: '',
        address: '',
        description: '',
        petName: '',
      },
      rules: {}
    }
  },

  methods: {
    // 跳转到宠物管理页
    gotoSkuManagement() {
      this.$router.push({
        path: "/sys-admin/product/pet",
        query: { name },
      });
    },

    standardByName(pet) {
      let url = 'http://localhost:9080/client/standardbypetname?name=' + pet.petName;
      console.log('url = ' + url);

      let localJwt = localStorage.getItem('localJwt');
      this.axios
          .create({
            'headers': {
              'Authorization': localJwt
            }
          })
          .get(url).then((response) => {
        let title = '提示';
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.pet = jsonResult.data;
          this.dialogVisible = true;
        } else {
          this.$confirm(jsonResult.message, title, {
            confirmButtonText: '添加宠物信息',
            cancelButtonText: '确认',
            type: 'warning'
          }).then(() => {
            this.$router.push('/sys-admin/product/pet/add-new?name='+this.name);

          }).catch(() => {
            this.loadClientList();
          });
        }
      });
    },
    // 返回
    goBack() {
      this.wd = '';
      this.paginationShow = true;
      this.willShow = false;
      this.loadClientList();

    },
    //模糊查询
    query() {
      if (this.wd.trim() == "") {
        // alert("请输入搜索的关键字!");
        this.$message.error("请输入搜索的关键字!");
        return;
      }
      let url = 'http://localhost:9080/client/search?wd=' + this.wd;
      console.log('url = ' + url);

      let localJwt = localStorage.getItem('localJwt');

      this.axios.create({
        'headers': {
          'Authorization': localJwt
        }
      }).get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.tableData = jsonResult.data;
          this.paginationShow = false;
          this.willShow = true;
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadClientList();
            }
          });
        }
      });
    },
    // 切换vip状态
    toggleVip(category) {
      let vipText = ['禁用', '启用'];
      let url = 'http://localhost:9080/client/';
      let vip = category.vip;
      if (vip == 1) {
        url += 'setVip?id=' + category.id;
      } else {
        url += 'disable?id=' + category.id;
      }
      console.log('url = ' + url);

      let localJwt = localStorage.getItem('localJwt');
      this.axios
          .create({'headers': {'Authorization': localJwt}})
          .post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '将【' + category.name + '】的vip状态修改为【' + vipText[vip] + '】成功！',
            type: 'success'
          });
          this.loadClientList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.client();
            }
          });
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    //根据id查询
    openEditDialog(client) {
      let url = 'http://localhost:9080/client/standard?id=' + client.id;
      console.log('url = ' + url);

      let localJwt = localStorage.getItem('localJwt');

      this.axios.create({
        'headers': {
          'Authorization': localJwt
        }
      }).get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.editForm = jsonResult.data;
          this.dialogFormVisible = true;
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadClientList();
            }
          });
        }
      });
    },
    //修改
    submitEditForm() {
      let url = 'http://localhost:9080/client/update';
      console.log('url = ' + url);

      let formData = this.qs.stringify(this.editForm);
      console.log('formData = ' + formData);

      let localJwt = localStorage.getItem('localJwt');

      this.axios.create({
        'headers': {
          'Authorization': localJwt
        }
      }).post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '修改客户成功！',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.loadClientList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadClientList();
            }
          });
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    //打开删除确认框
    openDeleteConfirm(client) {
      this.$confirm('此操作将永久删除【' + client.id + '-' + client.name + '】客户，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(client);
      }).catch(() => {
      });
    },
    //处理删除
    handleDelete(client) {
      let url = 'http://localhost:9080/client/delete';
      console.log('url = ' + url);

      let formData = 'id=' + client.id;
      console.log('formData = ' + formData);

      let localJwt = localStorage.getItem('localJwt');

      this.axios.create({
        'headers': {
          'Authorization': localJwt
        }
      }).post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '删除客户成功！',
            type: 'success'
          });
          this.loadClientList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadClientList();
            }
          });
        } else if (jsonResult.state == 40900) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    //页面
    changePage(value) {
      this.$router.replace('?page=' + value);
      this.loadClientList();
    },
    //加载列表
    loadClientList() {
      let page = this.$router.currentRoute.query.page;
      if (!page) {
        page = 1;
      }

      let url = 'http://localhost:9080/client/list?page=' + page;
      console.log('url = ' + url);

      let localJwt = localStorage.getItem('localJwt');

      this.axios.create({
        'headers': {
          'Authorization': localJwt
        }
      }).get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.tableData = jsonResult.data.list;
          this.total = jsonResult.data.total;
          this.pageSize = jsonResult.data.pageSize;
          this.currentPage = jsonResult.data.currentPage;
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    }
  },
  mounted() {

    this.loadClientList();
    this.data = this.tableData;
  }
}
</script>